package portal.mybatis.dao.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import portal.mybatis.dao.NodeDetail;
import portal.mybatis.dao.NodeDetailExample;
import portal.mybatis.page.Page;
import portal.mybatis.dao.mapper.NodeDetailMapper;

@Service
public class VNodeService {
	private static final String[] props={"nodeIp","status","nodeName","gpuIdentify","groupName"};
	private static final String[] dcols={"node_ip","status","node_name","gpu_identify","group_name"};
	@Autowired
	NodeDetailMapper nodeDetailMapper;
	/***********
	 * 此方法采用Mybatis Example 实现SQL 不利于维护，不推荐使用。
	 * *******/
	@Deprecated
	public Page<NodeDetail> selectNodesByPagesAndPageSize(int pageSize,int offset){

		NodeDetailExample example= new NodeDetailExample();
		long count =nodeDetailMapper.countByExample(example);

		Page<NodeDetail> page =new Page<NodeDetail>();
		page.setTotal(count);
		page.setPageSize(pageSize);
		example.setOrderByClause("node_id asc limit "+ offset + " , "+pageSize);
		List<NodeDetail>list =nodeDetailMapper.selectByExample(example);	
		if(list==null || list.size()==0)
			return null;
		//page.setPage(pageNow);
		page.setRows(list);
		return page;
	}
	public Page<NodeDetail> selectNodesByPage(Page<NodeDetail> page){		
		if(page.getSort()!=null &&  page.getSort().length()>0)
			page.setOrders(findOrderCol(page.getSort())+ " " +page.getSortOrder() );	
		List<NodeDetail>list =nodeDetailMapper.selectByPage(page);	
		if(list==null || list.size()==0){
			return null;
		}
		page.setRows(list);
		page.setTotal(nodeDetailMapper.countByPage(page));
		return page;
	}
	public String findOrderCol(String sort){
		for(int i=0;i<props.length;i++){
			if(props[i].equals(sort))
				return dcols[i];
		}
		return null;
	}
}
